import Opt from '@/components/dt/info/Opt.js'
import ruleOpt from '@/biz/option/rule.js'
import toolMgr from '@/biz/base/tool.js'

class ConfirmOpt extends Opt {
  constructor(info) {
    super(info)
    this.info = info
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    let accessory = this.accessory()
    let confirmDate = {
      title: '确认日期',
      placeholder: '请选择确认日期',
      key: 'confirmDate',
      date: {},
      visible: this.info.voucherAttachType != '7',
      rules: [ruleOpt.required('请选择确认日期')]
    }
    let voucherConfirmAmount = {
      title: '凭证确认金额',
      key: 'voucherConfirmAmount',
      visible: this.info.voucherAttachType != '7',
      group: [
        {
          key: 'voucherConfirmAmount',
          placeholder: '请填写凭证确认金额',
          col: 21,
          input: {
            type: 'number'
          }
        },
        {
          placeholder: '元',
          col: 3,
          text: {}
        }
      ],
      rules: [ruleOpt.required('请填写凭证确认金额', 'number')]
    }
    let remark = {
      title: '备注',
      key: 'voucherRemark',
      visible: this.info.voucherAttachType == '6' || this.info.voucherAttachType == '7',
      input: {
        type: 'textarea',
        autosize: {
          minRows: 2
        }
      },
      rules: [ruleOpt.required('请填写备注'), ruleOpt.max(300)]
    }
    let delay = {
      title: '延期时间',
      key: 'delayTime',
      visible: this.info.voucherAttachType == '7',
      select: {
        key: 'delayTime'
      },
      rules: [ruleOpt.required('请选择延期时间')]
    }
    this.all = [
      {
        title: '附件类型',
        placeholder: '请选择附件类型',
        key: 'voucherAttachType',
        select: {
          key: 'voucherAttachType'
        },
        rules: [ruleOpt.required('请选择附件类型')],
        change: (info) => {
          if (info.value == '6') {
            confirmDate.visible = true
            voucherConfirmAmount.visible = true
            accessory.visible = false
            remark.visible = true
            delay.visible = false
          } else if (info.value == '7') {
            confirmDate.visible = false
            voucherConfirmAmount.visible = false
            accessory.visible = false
            remark.visible = true
            delay.visible = true
          } else {
            confirmDate.visible = true
            voucherConfirmAmount.visible = true
            accessory.visible = true
            remark.visible = false
            delay.visible = false
          }
        }
      },
      confirmDate,
      voucherConfirmAmount,
      accessory,
      delay,
      remark
    ]
  }

  accessory() {
    let api = toolMgr.unloadFile()
    let files = []
    if (this.info.voucherAttach && this.info.voucherAttach.length > 0) {
      this.info.voucherAttach.forEach((item) => {
        if (item) {
          files.push({
            name: item.fileName,
            url: item.attachAddress
          })
        }
      })
    }
    return {
      title: '凭证',
      key: 'voucherAttach',
      upload: {
        title: '上传文件',
        api,
        files,
        multiple: true
      },
      visible: this.info.voucherAttachType != '6' && this.info.voucherAttachType != '7',
      data: {
        to: (v) => {
          return {
            voucherAttach: v.map((item) => {
              return {
                fileName: item.name,
                attachAddress: item.response ? item.response.data.imageURL : item.url
              }
            })
          }
        }
      },
      rules: [
        {
          required: true,
          message: '请上传双方盖章凭证',
          type: 'array',
          trigger: 'blur'
        }
      ]
    }
  }
}

export default ConfirmOpt
